package icegps.com.bhmap.drawhelp;

import android.animation.ValueAnimator;
import android.view.animation.LinearInterpolator;

import icegps.com.bhmap.App;
import icegps.com.bhmap.bean.MultipleEvaluatorHelp;
import icegps.com.bhmap.bean.TargetPoint;
import icegps.com.bhmap.evaluator.MultipleEvaluator;
import icegps.com.bhmap.evaluator.TargetPointEvaluator;
import icegps.com.bhmap.gen.TargetPointDao;
import icegps.com.bhmap.listener.OnUiChangeListener;
import icegps.com.bhmap.view.IMapView;

/**
 * Created by 111 on 2017/12/8.
 */

public class AnimaHelp {
    private OnUiChangeListener onUiChangeListener;
    private TargetPointDao targetPointDao;
    private int animTime;
    public static float ANIMA_END = 1.0F;//动画结束
    public static float NO_AINIMA = 2.0F;//没走动画
    private IMapView iMapView;

    public AnimaHelp(IMapView iMapView) {
        this.iMapView = iMapView;
        targetPointDao = App.getInstance().getDaoSession().getTargetPointDao();
        animTime = 200;
    }

    public void startLocationChange(final TargetPoint startPoint, final TargetPoint endPoint) {
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new TargetPointEvaluator(), startPoint, endPoint);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                TargetPoint targetPoint = (TargetPoint) animation.getAnimatedValue();
                float animatedFraction = animation.getAnimatedFraction();
                if (onUiChangeListener != null) {
                    onUiChangeListener.onLocationChange(targetPoint, animatedFraction);
                }
                if (animatedFraction == 1.0f) {
                    int x = (int) (endPoint.getPointX() / iMapView.getRLength());
                    int y = (int) (endPoint.getPointY() / iMapView.getRLength());
                    TargetPoint point = new TargetPoint();
                    point.setId(TargetPoint.getHashId(x, y));
                    point.setPointX(x);
                    point.setPointY(y);
                    point.setHeight(endPoint.getHeight());
                    targetPointDao.insertOrReplace(point);
                }

            }
        });
        valueAnimator.setDuration(animTime);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }


    public void startMultipleChange(MultipleEvaluatorHelp startValue, MultipleEvaluatorHelp endValue) {

        ValueAnimator valueAnimator = ValueAnimator.ofObject(new MultipleEvaluator(), startValue, endValue);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                MultipleEvaluatorHelp multipleEvaluatorHelp = (MultipleEvaluatorHelp) animation.getAnimatedValue();
                if (onUiChangeListener != null) {
                    onUiChangeListener.onMultipleChange(multipleEvaluatorHelp, animation.getAnimatedFraction());
                }
            }
        });
        valueAnimator.setDuration(animTime);
        valueAnimator.start();
    }

    public void setOnUiChangeListener(OnUiChangeListener onUiChangeListener) {
        this.onUiChangeListener = onUiChangeListener;
    }
}
